<?php
	include("include_header.php");
?>
<html>
<head>
	<title>GradeGrid - Account Home</title>
	<?php include("header.php"); ?>
</head>
<body onLoad="writeCopyright()">
	<div class="main">
		
		<?php
			include("write_functions.php");
			connect("","","","");
			include("boilerplate.php");
		?>
		
		<?php if($_SESSION["logged_in"]): ?>
			
			<!-- BEGIN MAIN CONTENT -->
			
			<?php if($_SESSION["student"]): ?>
			<?php
				$username = mysql_real_escape_string($_SESSION["username"]);
				$query = mysql_query("SELECT account_id FROM account WHERE username='$username'") or die(mysql_error());
				$account_id = mysql_fetch_array($query);
				$account_id = $account_id["account_id"];
				// At this point, nasa $account_id na yung account id ng logged in user! YAY
				
				// GET ALL REQS
				$query1 = "SELECT DISTINCT class_name, major.name AS 'major_name', minor.name AS 'minor_name', minor.minor_id, class.class_id " .
					"FROM take, class, major, minor, template, grade " .
					"WHERE minor.major_id = major.major_id " .
					"AND class.template_id = template.template_id " .
					"AND grade.minor_id = minor.minor_id " .		// Get only reqs that have already been GIVEN by instructor
					"AND take.class_id = class.class_id " .
					"AND take.account_id = '" . mysql_real_escape_string($account_id) . "' " .  
					"ORDER BY class.class_id, minor.minor_id";
				// Get ALL GRADES
				$query2 = "SELECT DISTINCT minor.minor_id AS 'minor_id', class.class_id AS 'class_id', grade " . 
					"FROM take, class, major, minor, template, grade " . 
					"WHERE grade.account_id = '" . mysql_real_escape_string($account_id) . "' " .
					"AND grade.class_id = class.class_id " . 
					"AND grade.major_id = major.major_id " . 
					"AND grade.minor_id = minor.minor_id " .
					"AND minor.major_id = major.major_id " . 
					"AND class.template_id = template.template_id " . 
					"AND take.class_id = class.class_id " . 
					"AND take.account_id = '" . mysql_real_escape_string($account_id) . "' " .
					"ORDER BY class.class_id, minor.minor_id";
				//echo $query2;
				
				// We now have two queries
				$all_reqs_resource = mysql_query($query1) or die(mysql_error()); 
				$all_reqs = array();
				$i = 0;
				while ($row = mysql_fetch_array($all_reqs_resource, MYSQL_ASSOC)) {
					$all_reqs[$i++] = $row;
				}
				$graded_reqs_resource = mysql_query($query2) or die(mysql_error()); 
				$graded_reqs = array();
				$j = 0;
				while ($row = mysql_fetch_array($graded_reqs_resource, MYSQL_ASSOC)) {
					$graded_reqs[$j++] = $row;
				}
				if ($j > $i) {
					die("<p>Database error.</p>");			// This should be impossible. 
				}
				//mysql_close($get_reqs);
			?>
			
				<div class="form_section"><h1>Your Classes</h1></div>
				<p>Your classes and grades: </p>
				
				<table class='schedule pMirror'>
					<tr>
					<td class='Schedule'><b>Class</b></td>
					<!--<td class='Schedule'><b>Major Requirement</b></td>-->
					<td class='Schedule'><b>Minor Requirement</b></td>
					<td class='Schedule'><b>Grade</b></td>
					</tr>
			
				<?php
					$i = 0;			// List ALL reqs with corresponding classes
					$j = 0; 		// Counter for reqs WITH grades only. Increment IF this entry matches something from the class reqs list
					while ($i < count($graded_reqs_resource)) {
						mysql_data_seek($graded_reqs_resource, $i);
						$graded_requirements = mysql_fetch_array($graded_reqs_resource);
						$classid = $graded_requirements["class_id"];
						echo "<tr class='schedule'>";
						$classname_query = mysql_query("SELECT course_name, class_name FROM course, class WHERE class_id = '$classid' AND
						                               course.course_id = class.course_id");
						//echo "SELECT course_name, class_name FROM course, class WHERE class_id = '$classid' AND
						  //                             course.course_id = class.course_id";
						$classname_array = mysql_fetch_array($classname_query);
						echo "<td class='schedule'>" . $classname_array["course_name"] . " - " . $classname_array["class_name"] . "</td>";
						
						$minorid = $graded_requirements["minor_id"];
						$minorname_query = mysql_query("SELECT name FROM minor WHERE minor_id = '$minorid'");
						$minorname_array = mysql_fetch_array($minorname_query);
						echo "<td class='schedule'>" . $minorname_array["name"] . "</td>";
						echo "<td class='schedule'>" . $graded_requirements["grade"] . "</td>";
						echo "</tr>";
						$i++;
					}
				?>
				</table>
			
			</div>
			<!-- END OF CONTENT -->
			<?php else: ?>
				<?php include("loginnavigation.php"); ?>
				<div class="left">
				<?php
					$username = mysql_real_escape_string($_SESSION["username"]);
					$name_query = sprintf("SELECT * FROM account WHERE username = '%s'",
									      $username);
					
					$tuple_table = mysql_query($name_query);
					$tuple = mysql_fetch_array($tuple_table);
					$name = $tuple["first_name"] . " " . $tuple["middle_name"] . " " . $tuple["last_name"];
					
					if($tuple["first_name"] == NULL){
						$u = $_SESSION["username"];
						echo "<h1>Welcome, $u</h1>";
					} else{
						echo "<h1>Welcome, $name</h1>";
					}
				?>
				</div>
			<?php endif ?>
			
		<?php else: ?>
			<?php executeFile("../files/access_error.txt"); ?>
		<?php endif ?>
	</div>
	<div id="copywright"></div>
</body>
</html>